<template>
	<div class="register">
		<Header>
            <span>注册</span>
        </Header>
		<section>
				<div class="register-tel">
					<input type="text" v-model="userTel" placeholder="请输入手机号码" pattern="[0-9]" />
				</div>
				<div class="register-code">
					<input type="text" placeholder="请输入验证码" pattern="[0-9]" v-model="userCode" />
					<button 
					:disabled="disabled"
					@click="sendCode"
					>
					{{codeMsg}}
					</button>
				</div>
                <div class="register-pwd">
                    <input type="text" v-model="userPwd" placeholder="请设置密码">
                </div>
                <!-- <div class="register-rpwd">
                    <input type="text" placeholder="请再次确认密码">
                </div> -->
				<div class="register-btn" @click="register">注 册</div>
		</section>
		<Tabbar></Tabbar>
	</div>
</template>

<script>
import  Tabbar  from "@/components/common/Tabbar.vue";
import  Header  from "@/views/login/Header.vue";
import { Toast } from 'mint-ui';
import http from '@/common/api/request.js';
export default{
	data(){
		return{
			disabled:false,
			userTel:'',
			userCode:'',
            userPwd:'',
			rules:{
				//手机号验证规则
				userTel:{
					rule:/^1[23456789]\d{9}$/,
					msg:'手机号不能为空，并且是11位数字',
					
				},
                userPwd:{
                    rule:/^\w{6,12}$/,
					msg:'密码不能为空，并且为6-12位',
                }
			},
			codeNum:6,
			codeMsg:'获取短信验证码',
			code:'',
            
		}
	},
	components: {
		Tabbar,
		Header,
	},
	methods:{
		//获取短信验证码按钮
		sendCode(){
			if( !this.validate('userTel') ) return;
			//请求短信验证码接口
			http.$axios({
				url:'/api/code',
				method:'POST',
				data:{
					phone:this.userTel
				}
			}).then(res=>{
				if(res.success){
					this.code = res.data;
				}
				
			})

			//禁用按钮
			this.disabled = true;
			//倒计时
			let timer = setInterval(()=>{
				--this.codeNum;
				this.codeMsg = `重新发送${this.codeNum}`;
			},1000)
			//判断什么时候停止定时器
			setTimeout(()=>{
				clearInterval(timer);
				//将倒计时恢复到6并且按钮可用,文本仍然为获取短信验证码
				this.codeNum = 6;
				this.disabled = false;
				this.codeMsg = '获取短信验证码';
			},6000)
		},
		//跳转到快捷注册
		goRegister(){
			this.$router.push('/register');
		},
		//跳转到了密码登录
		goUserLogin(){
			//验证
			this.$router.push('/userLogin');
		},
        //验证信息提示
		validate( key ){
			let bool = true;
			if( !this.rules[key].rule.test( this[key]) ){
				//提示信息
				Toast(this.rules[key].msg);
				bool = false;
				return false;
			}
			return bool;
		},
		//点击注册
		register(){
            //验证密码
            if( !this.validate('userPwd') )  return;
            //判断验证码是否正确
            if( this.code != this.userCode ){
                Toast('验证码不正确');
                return;
            }
             //如果正确，进行注册
			//证明用户输入的短信验证码正确的
			//发送请求
			http.$axios({
				url:'/api/register',
				method:'POST',
				data:{
					phone:this.userTel,
                    pwd:this.userPwd,
				}
			    }).then(res=>{
					console.log(res);
			})
	
		},
		
	},
	
}
</script>

<style scoped>
.register{
	display: flex;
	flex-direction: column;
	width: 100vw;
	height: 100vh;
	overflow: hidden;
}
section{
	display: flex;
	flex-direction: column;
	background-color: #f5f5f5;
	align-items: center;
}
section div{
	margin: 0.625rem 0;
	width: 20.9375rem;
	height: 2.75rem;
	
}
.register-tel{
	margin-top: 1.25rem;
}
.register-tel input{
	width: 100%;
}
input{
	box-sizing: border-box;
	padding: 0 0.625rem;
	background-color: #fff;
	line-height: 2.75rem;
	border: 0.0625rem solid #ccc;
	border-radius: 0.375rem;
	
	
}
.register-code {
	display: flex;	
}
.register-code input{
	flex: 1;
}
.register-code button{
	padding: 0 1.25rem;
	line-height: 2.75rem;
	color: #fff;
	background-color: green;
	border: 0;
	border-radius: 0.375rem;
}
.register-pwd input,.register-rpwd input{
    width: 100%;
}
.register-btn{
	line-height: 2.75rem;
	color: #fff;
	text-align: center;
	background-color: green;
	border-radius: 0.375rem;
	
}

</style>